package com.zmn.plat.admin.controller.common.upload;

import java.io.IOException;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.business.interfaces.pic.PicUploadBService;
import com.zmn.plat.model.vo.mast.FileUploadReslutDTO;

/**
 * 通用文件上传
 *
 * @author linfeng
 * @version v1.0
 * @since 2020/02/17 14:18
 */
@Controller
@RequestMapping("/common/")
public class FileUploadController {

    private Logger logger = LoggerFactory.getLogger(FileUploadController.class);

    /**
     * 通用文件路经
     */
    private static final String COMMON_FILE_PATH = "/common/file/";

    @Resource
    PicUploadBService picUploadBService;

    /**
     * 上传附件
     * 
     * @param file
     * @param folderName
     *            自己功能的目录名称，方便区分使用
     * @param fileName
     *            自己定义的文件名称，如果该参数未传，则使用文件自己的名称
     * @return
     */
    @RequestMapping(value = "uploadFile")
    @ResponseBody
    public FileUploadReslutDTO uploaderFile(@RequestParam(value = "file") MultipartFile file,
        @RequestParam(value = "folderName", required = false) String folderName,
        @RequestParam(value = "fileName", required = false) String fileName) {
        String directory = COMMON_FILE_PATH;
        if (StringUtil.isNotBlank(folderName)) {
            directory = folderName;
        } else {
            directory += "default";
        }

        FileUploadReslutDTO fileUploadReslutDTO = new FileUploadReslutDTO();
        FileUploadReslutDTO.Data data = new FileUploadReslutDTO.Data();
        if (StringUtil.isBlank(fileName)) {
            fileName = ((CommonsMultipartFile)file).getFileItem().getName();;
        } else {
            String originFileName = ((CommonsMultipartFile)file).getFileItem().getName();
            String[] array = originFileName.split("\\.");
            if (array != null && array.length > 1) {
                fileName = fileName + "." + array[1];
            }
        }
        Long fileSize = ((CommonsMultipartFile)file).getFileItem().getSize();

        String url = picUploadBService.getPicUrl(COMMON_FILE_PATH, fileName);

        try {
            ResultDTO rel = picUploadBService.uploadFile(directory, fileName, file.getInputStream());
            if (rel.getStatusBool()) {
                fileUploadReslutDTO.setCode(GlobalDict.RESULT_SUCCESS);
                fileUploadReslutDTO.setMsg(GlobalConsts.SUCCESS_MSG);
                data.setSrc(picUploadBService.getPicUrl(directory, fileName));
                data.setName(fileName);
                data.setSize(fileSize);
                fileUploadReslutDTO.setData(data);
                logger.info("[zmn]上传附件成功，URL: {}", url);
                return fileUploadReslutDTO;
            }
            return fileUploadReslutDTO;
        } catch (IOException e) {
            logger.error("[zmn]上传附件异常，message:{}", e.getMessage());
            return new FileUploadReslutDTO(GlobalDict.RESULT_FAIL, "文件上传失败", null);
        }
    }
}
